package com.label.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.label.entity.Labels;
import com.label.entity.Log;
import com.label.entity.Patientlabels;
import com.label.mapper.LabelsMapper;
import com.label.service.LabelsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.label.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jianhua
 * @since 2022-03-27
 */
@Service
public class LabelsServiceImpl extends ServiceImpl<LabelsMapper, Labels> implements LabelsService {
    @Autowired
    LogService logService;
    @Autowired
    private LabelsMapper labelsMapper;
    @Autowired
    HttpSession session;
    @Override
    public List<Labels> labelList(){
        QueryWrapper<Labels> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("labelstate",1);
        List listlabels =labelsMapper.selectList(queryWrapper);
        System.out.println(listlabels);
        return listlabels;
    }
    @Override
    public Page pageList(Page page, Labels labels){
        if (page==null){
            page =new Page();
        }
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("labelstate",1);
        if (labels!=null&& StringUtils.isNotEmpty(labels.getLabelname())){
            wrapper.like("labelname",labels.getLabelname());


        }
        return super.page(page,wrapper);
    }
@Override
    public Labels getById(Integer id){
    QueryWrapper<Labels> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("idlabels", id);
    queryWrapper.eq("labelstate",1);
    Labels labels = labelsMapper.selectOne(queryWrapper);
    System.out.println(labels);
    return labels;
}
@Override
public List<Labels> getBytype(String type){
        QueryWrapper<Labels> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("labeltype",type);
        queryWrapper.eq("labelstate",1);
        List listlabels =labelsMapper.selectList(queryWrapper);
    System.out.println(listlabels);
    return listlabels;

}
@Override
    public boolean saveOrUpdate(Labels labels){
        int i=labelsMapper.insert(labels);
    if (i>0){
        //        日志记录
        Log log=new Log();
        log.setUsername((String) session.getAttribute("username"));
        log.setLabelid(labels.getIdlabels());
        log.setOperationType("添加标签");
        log.setLabelname(labels.getLabelname());
        log.setOperationtime(LocalDateTime.now());
        System.out.println(log);
        logService.logsave(log);
        return true;
    }else return false;
}
@Override
public boolean update(Labels labels){
        int i= labelsMapper.updateById(labels);
    if (i>0){
        //        日志记录
        Log log=new Log();
        log.setUsername((String) session.getAttribute("username"));
        log.setLabelid(labels.getIdlabels());
        log.setOperationType("修改标签");
        log.setLabelname(labels.getLabelname());
        log.setOperationtime(LocalDateTime.now());
        System.out.println(log);
        logService.logsave(log);
        return true;
    }else return false;
}
@Override
    public boolean updateById( Labels labels){

        int i=labelsMapper.updateById(labels);
    if (i>0){
        //        日志记录
        Log log=new Log();
        log.setUsername((String) session.getAttribute("username"));
        log.setLabelid(labels.getIdlabels());
        log.setOperationType("删除标签");
        log.setLabelname(labels.getLabelname());
        log.setOperationtime(LocalDateTime.now());
        System.out.println(log);
        logService.logsave(log);
        return true;
    }else return false;
}
@Override
    public Page getBykey(Page page,String key){
    if (page==null){
        page =new Page();
    }
    QueryWrapper wrapper=new QueryWrapper();
    wrapper.eq("labelstate",1);
    wrapper.like("labelname",key);
    if (key!=null&&StringUtils.isNotEmpty(key)){
        wrapper.like("labelname",key);
    }
    return super.page(page,wrapper);
}
}
